package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {

  /*  @Reference
    private IStrategyCommendService strategyCommendService;*/
     //因为在登录按钮的前端页面上绑定的跳转匹配路径是“/”，为了匹配，我们想让他
    //跳转到index.ftl页面，就需要加上“”，这样才是匹配requestMapping路径为“/”


    //使用Springmvc的对象注入方式
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private ITravelService travelService;

    /**
     * 进去首页-显示五条推荐数据
     * @return
     */
    @RequestMapping("")
    public String index(Model model){
        List <TravelCommend> list = travelCommendService.queryForContentTop5();
        model.addAttribute("tcs",list);
        //遍历list里面的数据,因为都是list的数据,取第一个
        model.addAttribute("scs",list.subList(0,1));
        return "index/index";
    }
    @RequestMapping("/userInfo2")
    @ResponseBody
    public UserInfo getUserInfo2(HttpServletRequest request, UserInfo userInfo){
        return userInfo;
    }

    //使用自定义的对象注入方式
    @RequestMapping("/userInfo")
    @ResponseBody
    public UserInfo getUserInfo(HttpServletRequest request, @UserParam UserInfo userInfo){
        return userInfo;
    }


    @RequestMapping("/index/travelPage")
    public String travelPage (Model model, @ModelAttribute("qo")TravelQuery qo){
        //pageInfo
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        return "index/travelPageTpl";
    }
}
